<?php
// +----------------------------------------------------------------------
// | Bwsaas
// +----------------------------------------------------------------------
// | Copyright (c) 2015~2020 http://www.buwangyun.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Gitee ( https://gitee.com/buwangyun/bwsaas )
// +----------------------------------------------------------------------
// | Author: buwangyun <hnlg666@163.com>
// +----------------------------------------------------------------------
// | Date: 2020-9-28 10:55:00
// +----------------------------------------------------------------------

namespace app\manage\controller\admin;

use app\manage\model\admin\CodeDictData;
use app\manage\model\admin\CodeDictType;
use buwang\base\AdminBaseController;
use buwang\traits\CrudControllerTrait;


class CodeDictController extends AdminBaseController
{
    use CrudControllerTrait;

    protected $model = null;//模型实例

    protected function initialize()
    {
        parent::initialize();
    }

    /**
     * 获取字典类型列表
     */
    public function dictType()
    {
        $page = request()->get('page/d', 1);
        $page_type = request()->get('page_type/d', 0);
        $limit = request()->get('limit/d', 10);
        $name = request()->get('name');
        $type = request()->get('type');
        $status = request()->get('status');

        //不分页
        if($page_type){
            $limit = 25000;
        }

        $where = [];
        if (!empty($name)) {
            $where['name'] = $name;
        }
        if (!empty($type)) {
            $where['type'] = $type;
        }
        if ($status != '') {
            $where['status'] = $status;
        }

        $data = [
            'lists' => CodeDictType::where($where)
                ->append(['status_desc'])
                ->order(['id' => 'desc'])
                ->page($page, $limit)
                ->select()
                ->toArray(),
            'count' => CodeDictType::where($where)->count(),
        ];
        return $this->success('successful', $data);
    }

    /**
     * 新增字典类型
     */
    public function addDictType()
    {
        $params = request()->post();
        $rule = [
            'name' => 'require',
            'type' => 'require',
            'status' => 'require|in:0,1',
        ];
        $this->validate($params, $rule);
        CodeDictType::create($params);
        return $this->success('successful');
    }

    /**
     * 编辑字典类型
     */
    public function editDictType()
    {
        $params = request()->post();
        $rule = [
            'id' => 'require',
            'name' => 'require',
            'type' => 'require',
            'status' => 'require|in:0,1',
        ];
        $this->validate($params, $rule);

        $detail = CodeDictType::findOrEmpty($params['id'])->toArray();
        if (!$detail) {
            return $this->error('数据不存在');
        }

        CodeDictType::update([
            'id' => $params['id'],
            'name' => $params['name'],
            'type' => $params['type'],
            'status' => $params['status'],
            'remark' => $params['remark'] ?? '',
        ]);
        CodeDictData::where(['type_id' => $params['id']])->update(['type_value' => $params['type']]);

        return $this->success('successful');
    }

    /**
     * 删除字典类型
     */
    public function deleteDictType()
    {
        $params = request()->post();
        $rule = [
            'id' => 'require',
        ];
        $this->validate($params, $rule);

        $dictData = CodeDictData::whereIn('type_id', $params['id'])->select();
        foreach ($dictData as $item) {
            if (!empty($item)) {
                return $this->error('典类型已被使用，请先删除绑定该字典类型的数据');
            }
        }
        CodeDictType::destroy($params['id']);
        return $this->success('successful');
    }

    /**
     * 获取字典数据
     */
    public function dictData()
    {
        $page = request()->get('page/d', 1);
        $limit = request()->get('limit/d', 10);
        $type_id = request()->get('type_id');
        $name = request()->get('name');
        $status = request()->get('status');

        $where = [];
        if (!empty($type_id)) {
            $where['type_id'] = $type_id;
        }
        if (!empty($name)) {
            $where['name'] = $name;
        }
        if ($status != '') {
            $where['status'] = $status;
        }

        $data = [
            'lists' => CodeDictData::where($where)
                ->append(['status_desc'])
                ->order(['id' => 'desc'])
                ->page($page, $limit)
                ->select()
                ->toArray(),
            'count' => CodeDictData::where($where)->count(),
        ];
        return $this->success('successful', $data);
    }

    /**
     * 新增字典数据
     */
    public function addDictData()
    {
        $params = request()->post();
        $rule = [
            'name' => 'require|length:1,255',
            'value' => 'require',
            'type_id' => 'require',
            'status' => 'require|in:0,1',
        ];
        $this->validate($params, $rule);

        $article = CodeDictType::findOrEmpty($params['type_id']);
        if ($article->isEmpty()) {
            return $this->error('字典类型不存在');
        }

        $data = [
            'name' => $params['name'],
            'value' => $params['value'],
            'sort' => $params['sort'] ?? 0,
            'status' => $params['status'],
            'remark' => $params['remark'] ?? '',
            'type_id' => $params['type_id'],
            'type_value' => $article['type'],
        ];

        CodeDictData::create($data);

        return $this->success('successful');
    }

    /**
     * 新增字典数据
     */
    public function editDictData()
    {
        $params = request()->post();
        $rule = [
            'id' => 'require',
            'name' => 'require|length:1,255',
            'value' => 'require',
            'status' => 'require|in:0,1',
        ];
        $this->validate($params, $rule);

        $article = CodeDictData::findOrEmpty($params['id']);
        if ($article->isEmpty()) {
            return $this->error('字典数据不存在');
        }

        $data = [
            'name' => $params['name'],
            'value' => $params['value'],
            'sort' => $params['sort'] ?? 0,
            'status' => $params['status'],
            'remark' => $params['remark'] ?? '',
        ];

        CodeDictData::where(['id' => $params['id']])->update($data);

        return $this->success('successful');
    }

    /**
     * 删除字典数据
     */
    public function deleteDictData()
    {
        $params = request()->post();
        $rule = [
            'id' => 'require',
        ];
        $this->validate($params, $rule);

        CodeDictData::destroy($params['id']);
        return $this->success('successful');
    }


}
